<?php
class ctrl_price extends ctrl_base
{


    /**
     * 查看权限
     */
    public function visiable(){
        $openid = $_SESSION['wx_userinfo']['openid'];
        if(!empty($_SESSION['wx_userinfo']['user_type'])) {
            $visible = ctrl_news::get_visible_str($openid, $_SESSION['wx_userinfo']['user_type']);
        } else {
            $userInfo = model_user::get_user_by_openid($openid,WEIXINHAO);
            if(!empty($userInfo['user_type'])) {
                $visible = ctrl_news::get_visible_str($openid, $userInfo['user_type']);
            } else {
                $visible = "*_*";//微信可见用户，*_* 表示所有用户可见
            }
        }
        return $visible;
    }
	/**
	 * 产品分类
	 */
    public static function index() {

        self::weixin_check_login();

        $openid = $_SESSION['wx_userinfo']['openid'];
        $data = model_user::get_user_by_openid($openid,WEIXINHAO);
        if(isset($data['user_id']) && $data['user_id'] != '0') {
            self::smarty() -> assign('user_id',$data['user_id']);
        } else {
            $reg_url = HOST_URL.'/index.php?act=register';
            $msg = "  您好！您还未进行用户注册，不能享受此服务,请点击<a href='".$reg_url."'>这里</a>注册。";
            model_send::send_text($openid,$msg);
            self::smarty() -> display('price_index.html');
            exit;
        }

        $visible = self::visiable();

    	$price_class_list = model_price::get_price_class_list();
        //产品大类下有有效且配置了价格公式的小类


        foreach($price_class_list as $key=>$value){
            $num =model_price::countChild($value['product_class_id'],$visible);
            if($num == 0){
                unset($price_class_list[$key]);
            }
        }

    	self::smarty() -> assign('price_class_list',$price_class_list);
    	self::smarty() -> display('price_index.html');
    }

    /**
     *c
     *  获取小类
     */
    public static function child() {
        $id = isset($_GET['id'])? intval($_GET['id']):0;
        $name = trim($_GET['name']);
        if($id == 0) {
            self::smarty()->display('error/wx_error.html');
            exit;
        }
        //获取大类下小类
        self::weixin_check_login();
        $visible = self::visiable();
        $list = model_price::getChild($id,$visible);

        if(count($list) == 1){
          header('location:/index.php?act=price&st=query&id='.$list[0]['product_id']);
          exit;
        }
        self::smarty() -> assign('name',$name);
        self::smarty() -> assign('price_list',$list);
        self::smarty() -> display('price_child.html');

    }



        /**
	 * 产品信息录入
	 */
    public static function query() { 
    	$id = isset($_GET['id'])? intval($_GET['id']):0;
        $name = trim($_GET['name']);
    	if($id == 0) {
    		self::smarty()->display('error/wx_error.html');
    		exit;
    	}
        if(empty($name)){
            $name= model_price::getChildName($id);
        }


        //产品材质
        $marerials = model_price::get_marerials($id);
        $marerialsCount = count($marerials);
        self::smarty() -> assign('marerials',$marerials);
        self::smarty() -> assign('marerialsCount',$marerialsCount);

        //销售区域
        $area_price = model_price::get_area_price($id);
        $area_priceCount = count($area_price);
        self::smarty() -> assign('area_price',$area_price);
        self::smarty() -> assign('area_priceCount',$area_priceCount);


        //运输方式和到货地
        $arr_transport = model_price::get_transport($id);
        $arr_transportCount = count($arr_transport);
        self::smarty() -> assign('arr_transport',$arr_transport);
        self::smarty() -> assign('arr_transportCount',$arr_transportCount);


        if($arr_transportCount == 1){
            //查询出到货地址
            $transport_tmp_id_ = 0;
            foreach($arr_transport as $key=>$value){
                $transport_tmp_id_ = $value['transport_tmp_id'];
            }
            $addList = model_price::getArea($transport_tmp_id_);
            $addListCount = count($addList);
            self::smarty() -> assign('addList',$addList);
            self::smarty() -> assign('addListCount',$addListCount);
        }

        //自定义模块
        $model = model_price::getModel($id);

        //print_r($model);
        self::smarty() -> assign('model',$model);


        self::smarty() -> assign('name',$name);
        self::smarty() -> assign('id',$id);
    	self::smarty() -> display('price_query.html');
    }
    /**
     * 
     * 根据产品小类获取其他价格选项
     */
	public static function get_data() {
    	$product_id = isset($_GET['product_id'])? intval($_GET['product_id']):0;
    	if($product_id == 0) {
    		$result = array(
    			'errCode' => '01',
    			'errMsg' => '非法请求',
    			'items' => ''
    		);
    	} else {
    		$data['marerials'] = model_price::get_marerials($product_id);//产品材质
    		$area_price = model_price::get_area_price($product_id); //区域价格
    		$data['area_price'] = explode(",", $area_price);
    		$arr_transport = model_price::get_transport($product_id);
    		$data['transport'] = array();
    		$tranport_types = array(
    			'motor' => '汽运',
    			'shipping' => '船运',
    			'motor_shipping' => '汽船联运',
    			'train' => '火运'
    		);
    		foreach ($arr_transport as $key => $value) { //获取运输方式
    			if(!empty($value)) {
    				array_push($data['transport'], $key);
    			}
    		}
    		if(count($data['transport'])>0) {
	    		for($i=0;$i<count($data['transport']);$i++) {
	    			$transport_list['val'][$i] = $data['transport'][$i];
	    			$transport_list['name'][$i] = $tranport_types[$data['transport'][$i]];
	    		}
    		} else {
    			$transport_list = "";
    		}
    		$result = array(
    			'errCode' => '00',
    			'errMsg' => 'ok',
    			'marerials' => $data['marerials'], //基础材质
    			'area_price' => $data['area_price'], //区域定价
    			'transport' => $transport_list, //运输方式
    		);
    	}
    	framework_static_function::output_json($result,FALSE);
    }


    /**
     * 获取运输区域
     */
    public static function get_transport_area() {
        //查询出到货地址
        if(intval($_GET['id'])>0){
            $addList = model_price::getArea(intval($_GET['id']));
            $result = array(
                'errCode' => '00',
                'items' => $addList
            );
        }else{
            $result = array(
                'errCode' => '01',
                'errMsg' => '非法请求',
                'items' => ''
            );
        }
    	framework_static_function::output_json($result,false);
    }
	/**
	 * 价格查询结果展示
	 */
    public static function query_result() {

        self::smarty() -> assign("name",$_POST['name']);
        self::smarty() -> assign("number",$_POST['number']);
        //获取产品名称
        $id = $_POST['id'];

        $pre_money = empty($_POST['price'])?0:$_POST['price'];
        //产品材质
        $marerials = model_price::get_marerials($id);
        foreach($marerials as $key=>$value){
            if($_POST['marerials'] == $value['basic_price_id']){
                $pre_money += $value['markup'];
                //var_dump('材质--'.$value['markup']);
                self::smarty() -> assign("marerials",$value['marerial']);
            }
        }


        //销售区域
        $area_price = model_price::get_area_price($id);
        foreach($area_price as $key=>$value){
            if($_POST['area_price'] == $value['area_price_id']){
                $pre_money += $value['cur_price'];
                //var_dump('区域--'.$value['cur_price']);
            }
        }


        //运输方式和到货地

        $transport_type = '';
        $transport_id = '';
        $arr_transport = model_price::get_transport($id);
        foreach($arr_transport as $key=>$value){
            if($_POST['transport']==$value['transport_tmp_id']){
                $transport_type =  $value['tmp_type'];
                self::smarty() -> assign("transport",$value['tmp_name']);
            }
        }

        //查询出到货地址
        $addList = model_price::getArea($_POST['transport']);
        foreach($addList as $key=>$value){
            if($_POST['transport_type']==$value['id']){
                $transport_id = $value['id'];
                self::smarty() -> assign("address",$value['name']);
            }
        }

        //获取快递费用
        $transport_price = model_price::getTransportPrice($transport_type,$transport_id,$_POST['transport'],$_POST['number']);
        
	framework_static_function::write_log('运费:'.$transport_price.'--'.json_encode($_POST),'price');
	//var_dump('运费--'.$transport_price);
        //自定义模块
        $model = model_price::getModelResult($id);
        //隐藏的选项

        foreach($model as $onek =>$onev){
            if($onev['select_all'] == 0){
                foreach($onev['child'] as $twok=>$twov){
                    if($twov['childCount'] > 0){
                        $newk ='model_'.$twov['product_price_id'];
                        foreach($twov['child'] as $threek=>$threev){
                            if($_POST[$newk] == $threev['product_price_id']){
                                $model[$onek]['child'][$twok]['result'] = $threev['price_name'];
                            }
                        }
                        unset( $model[$onek]['child'][$twok]['child']);
                    }else{
                        $newk ='model_'.$onev['product_price_id'];
                        if($_POST[$newk] == $twov['product_price_id']){
                            $model[$onek]['result'] = $twov['price_name'];
                        }
                        unset( $model[$onek]['child'][$twok]);
                    }
                }
            }else{
                foreach($onev['child'] as $twok=>$twov){

                    if($twov['childCount'] > 0){
                        $newk ='model_'.$twov['product_price_id'];
                        foreach($twov['child'] as $threek=>$threev){
                            if($_POST[$newk] == $threev['product_price_id']){
                                $model[$onek]['child'][$twok]['result'] = $threev['price_name'];
                            }
                        }
                        unset( $model[$onek]['child'][$twok]['child']);
                    }else{
                        $newk ='model_'.$onev['product_price_id'];
                        if($_POST[$newk] == $twov['product_price_id']){
                            $model[$onek]['result'] = $twov['price_name'];
                        }
                        unset( $model[$onek]['child'][$twok]);
                    }
                }
            }

        }
        self::smarty() -> assign('model',$model);

        //计算模板费用
        $modelprice = model_price::getModel($id);
        //print_r($modelprice);
        //隐藏的选项
        $id_list ='';
        foreach($modelprice as $onek =>$onev){
            if($onev['select_all'] == 0){
                foreach($onev['child'] as $twok=>$twov){
                    if($twov['childCount'] > 0){
                        $newk ='model_'.$twov['product_price_id'];
                        foreach($twov['child'] as $threek=>$threev){
                            if($_POST[$newk] == $threev['product_price_id']){
                                //$modelprice[$onek]['child'][$twok]['result'] = $threev['product_price_id'];
                                if(empty($id_list)){
                                    $id_list = $threev['product_price_id'];
                                }else{
                                    $id_list .= ','.$threev['product_price_id'];
                                }

                            }
                        }
                        unset( $modelprice[$onek]['child'][$twok]['child']);
                    }else{
                        $newk ='model_'.$onev['product_price_id'];
                        if($_POST[$newk] == $twov['product_price_id']){
                            $model[$onek]['result'] = $twov['price_name'];
                            if(empty($id_list)){
                                $id_list = $twov['product_price_id'];
                            }else{
                                $id_list .= ','.$twov['product_price_id'];
                            }
                        }
                        unset( $modelprice[$onek]['child'][$twok]);
                    }
                }
            }else{
                foreach($onev['child'] as $twok=>$twov){
                    if($twov['childCount'] > 0){
                        $newk ='model_'.$twov['product_price_id'];
                        foreach($twov['child'] as $threek=>$threev) {

                            if ($_POST[$newk] == $threev['product_price_id']) {
                                //$modelprice[$onek]['child'][$twok]['result'] = $threev['product_price_id'];
                                if (empty($id_list)) {
                                    $id_list = $threev['product_price_id'];
                                } else {
                                    $id_list .= ',' . $threev['product_price_id'];
                                }
                            }
                            unset($modelprice[$onek]['child'][$twok]['child']);
                        }
                    } else{
                        $newk ='model_'.$onev['product_price_id'];
                        if($_POST[$newk] == $twov['product_price_id']){
                            $model[$onek]['result'] = $twov['price_name'];
                            if(empty($id_list)){
                                $id_list = $twov['product_price_id'];
                            }else{
                                $id_list .= ','.$twov['product_price_id'];
                            }
                        }
                        unset( $modelprice[$onek]['child'][$twok]);
                    }
                }
            }

        }

        //print_r($modelprice);
        $modelprice = 0;
        if($id_list){
            $modelprice = model_price::getModelPrice($id,$id_list);
        }
        //var_dump('模板价--'.$modelprice);
        $pre_money += $modelprice;
        $pre_money += $transport_price;

        self::smarty() -> assign("money", $pre_money);
        self::smarty() -> assign("allmoney", $pre_money*$_POST['number']);

    	self::smarty() -> display('price_query_result.html');
    }
       
}
